const maxProfit = prices => {
  const n = prices.length
  let ans = 0
  for (let i = 1; i < n; i++) {
    if (prices[i] > prices[i - 1]) {
      ans += (prices[i] - prices[i - 1])
    }
  }
  return ans
}

console.log(maxProfit([7, 1, 5, 3, 6, 4]));